package com.mes.controller.warehouse;

import com.interf.WCSUtils;
import com.mes.service.warehouse.XbkXjListService;
import com.yy.base.page.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("xbkXjList")
public class XbkXjListController {
    @Autowired
    private XbkXjListService xbkXjListService;

    //通过RFID号加载信息
    @ResponseBody
    @RequestMapping(value="selectXbkXjListByCode",method= RequestMethod.POST)
    public Message selectXbkXjListByCode(HttpServletRequest request){
        Message msg = new Message();
        String rfid = request.getParameter("code");   //RFID

        String code = rfid.replaceAll(" ", "");
        //区分传过来的code是物料号还是工单号
       // String type = "";

        //判断传过来的RFID是否在item表中存在
        List<Map<String, Object>> ListProduct = xbkXjListService.getBatchnorLists(code); //传过来的是RFID号

        if(ListProduct.size() == 0 ){
            msg.setData("xbkSjListByCode","false");
            return msg;
        }

        if(ListProduct.size() != 0){
           // type = "WL";
            msg.setData("PlanList",ListProduct);
        }

       // msg.setData("ListType",type);
        return msg;
    }


    //保存下架信息
    @ResponseBody
    @RequestMapping(value="saveXbkXjListByCode",method= RequestMethod.POST)
    public Message saveXbkXjListByCode(HttpServletRequest request){
        Message msg = new Message();
        try{
            String rfid = request.getParameter("code");   //RFID
            String qtys = request.getParameter("qtys");   //下架数量
            String code = rfid.replaceAll(" ", "");

            int numbers = Integer.parseInt(qtys);
            //区分传过来的code是物料号还是工单号
           // String type = "";

            //判断传过来的RFID是否在工单列表或者物料列表中存在
          //  List<Map<String, Object>> ListPlan = xbkXjListService.selectXbkPlanListByCode(code); //传过来的是作业工单号

          //  List<Map<String, Object>> ListProduct = xbkXjListService.selectXbkProductListByCode(code); //传过来的是物料号
            List<Map<String, Object>> ListProduct = xbkXjListService.getBatchnorLists(code); //传过来的是RFID号

            if(ListProduct.size() == 0 ){
                msg.setData("xbkXjListData","false");
                return msg;
            }

         /*   if(ListProduct.size() != 0){
                type = "WL";
            }else if(ListPlan.size() != 0){
                type = "PLAN";
            }*/
            List<Map<String, Object>> mescode = null;
            String ckCode = "";
          /*  if(type == "PLAN"){
                mescode = xbkXjListService.selectMesParameter("PATH1909090004"); //查询鞋面辅料MES参数

                ckCode = mescode.get(0).get("CSVALUE").toString();  //MES参数编码（仓库编码）

                //判读该作业工单是否已经上传至中间表
                int num  = WCSUtils.check(ckCode, code);

                //调用接口往中间表插数据
                WCSUtils.outStorck(ckCode, code, numbers);
                *//*if(num == 0) {
                    //调用接口往中间表插数据
                    WCSUtils.outStorck(ckCode, code, numbers);
                } else {
                    msg.setData("message","false");
                    return msg;
                }*//*
            }else if(type == "WL"){*/

                String WLID = ListProduct.get(0).get("WLID").toString();  //物料ID


                mescode = xbkXjListService.selectWarehouseCode(WLID);

                ckCode = mescode.get(0).get("CKCODE").toString();  //MES参数编码（仓库编码）

                //调用接口往中间表插数据
                WCSUtils.outStorck(ckCode, code,  numbers, code);
         //   }
        }catch (Exception e){
            msg.setData("result","false");
            e.printStackTrace();
        }
        return msg;
    }
}
